1669H - Maximal AND - CodeForces Solution


bitmasks greedy math *1300

Please click on ads to support us..

Python Code:

t = int(input())

while t:
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    cnt = [0] * 31
    for x in A:
        for i in range(31):
            cnt[i] += (1 - ((x >> i) & 1))
    for i in range(30, -1, -1):
        if k >= cnt[i]:
            k -= cnt[i]
            cnt[i] = 0


    ans = 0
    for i in range(0, 31):
        if cnt[i] == 0:
            ans += (1 << i)
    print(ans)
    t -= 1

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define vbe(v) v.begin(), v.end()
#define int long long
#define fi first
#define sec second
const int mod = 1e9 + 7;
const int N = 1e5 + 7;
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ch(n) cout << "hello-" << n << endl

void solve()
{
    int n, k;
    cin >> n >> k;
    vector<int> hsh(31, 0);
    int tt;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        cin >> tt;
        v.pb(tt);
        for (int ii = 30; ii >= 0; ii--)
        {
            if (((tt >> ii) & 1) == 1)
            {
                hsh[ii]++;
            }
        }
    }
    // for(auto &it:hsh){
    //    cout << it << ' ';
    // }
    // return;
    vector<pair<int, int>> vp;
    for (int i = 0; i < 31; i++)
    {
        vp.pb({i, n - hsh[i]});
    }
    reverse(vbe(vp));
    // for(auto &it:vp){
    //    cout << it.first << ' '<<it.second<<'\n';
    // }
    // return;
    // if(k>n){
    //    for (int i = 0; i < vp.size();i++){
    //       if(k>=vp[i].first && k>n){
    //       k = k - vp[i].first;
    //       val = val + (1 << vp[i].second);
    //       }
    //       else{
    //          break;
    //       }
    //    }
    // }
    vector<int> ans(31, 0);
    for (int i = 0; i < vp.size(); i++)
    {
        if (k >= vp[i].second)
        {
            k = k - vp[i].second;
            ans[vp[i].first]++;
        }
    }
    int val = 0;
    // int ct = 0;
    // for(auto &it:ans){
    //    cout << it<<' '<<ct << '\n';
    //    ct++;
    // }
    // cout << (1 << 31) << '\n';
    // return;
    // cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); i++)
    {
        if (ans[i])
        {
            val = val + (1 << i);
            // cout << val << '\n';
        }
    }
    // cout << val*2 << '\n';
    cout << val << '\n';
    // cout <<'\n';
    // cout << (1 << 8) << '\n';
}

signed main()
{

    //   #ifndef ONLINE_JUDJE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    //   #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int test = 0;
    cin >> test;
    while (test--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters